package org.ksoap2;

import java.io.IOException;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class SoapFault12 extends SoapFault
{
  private static final long serialVersionUID = 1012001L;
  public Node Code;
  public Node Reason;
  public Node Node;
  public Node Role;
  public Node Detail;

  public SoapFault12()
  {
    this.version = 120;
  }

  public SoapFault12(int version)
  {
    this.version = version;
  }

  public void parse(XmlPullParser parser)
    throws IOException, XmlPullParserException
  {
    parseSelf(parser);

    this.faultcode = this.Code.getElement("http://www.w3.org/2003/05/soap-envelope", "Value").getText(0);
    this.faultstring = this.Reason.getElement("http://www.w3.org/2003/05/soap-envelope", "Text").getText(0);
    this.detail = this.Detail;
    this.faultactor = null;
  }

  private void parseSelf(XmlPullParser parser) throws IOException, XmlPullParserException
  {
    parser.require(2, "http://www.w3.org/2003/05/soap-envelope", "Fault");

    while (parser.nextTag() == 2) {
      String name = parser.getName();
      parser.nextTag();
      if (name.equals("Code")) {
        this.Code = new Node();
        this.Code.parse(parser);
      } else if (name.equals("Reason")) {
        this.Reason = new Node();
        this.Reason.parse(parser);
      } else if (name.equals("Node")) {
        this.Node = new Node();
        this.Node.parse(parser);
      } else if (name.equals("Role")) {
        this.Role = new Node();
        this.Role.parse(parser);
      } else if (name.equals("Detail")) {
        this.Detail = new Node();
        this.Detail.parse(parser);
      } else {
        throw new RuntimeException("unexpected tag:" + name);
      }

      parser.require(3, "http://www.w3.org/2003/05/soap-envelope", name);
    }
    parser.require(3, "http://www.w3.org/2003/05/soap-envelope", "Fault");
    parser.nextTag();
  }

  public void write(XmlSerializer xw)
    throws IOException
  {
    xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Fault");

    xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Code");
    this.Code.write(xw);
    xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Code");
    xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Reason");
    this.Reason.write(xw);
    xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Reason");

    if (this.Node != null) {
      xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Node");
      this.Node.write(xw);
      xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Node");
    }
    if (this.Role != null) {
      xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Role");
      this.Role.write(xw);
      xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Role");
    }

    if (this.Detail != null) {
      xw.startTag("http://www.w3.org/2003/05/soap-envelope", "Detail");
      this.Detail.write(xw);
      xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Detail");
    }
    xw.endTag("http://www.w3.org/2003/05/soap-envelope", "Fault");
  }

  public String getMessage()
  {
    return this.Reason.getElement("http://www.w3.org/2003/05/soap-envelope", "Text").getText(0);
  }

  public String toString()
  {
    String reason = this.Reason.getElement("http://www.w3.org/2003/05/soap-envelope", "Text").getText(0);
    String code = this.Code.getElement("http://www.w3.org/2003/05/soap-envelope", "Value").getText(0);
    return "Code: " + code + ", Reason: " + reason;
  }
}
